package com.iknow.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.iknow.IKnow;
import com.iknow.app.IKnowSystemConfig;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.util.Regex;
import com.iknow.util.StringUtil;
import com.weibo.net.AccessToken;
import com.weibo.net.RequestToken;
import com.weibo.net.ShareActivity;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SinaBindActivity extends Activity
{
  public static final String URL_ACTIVITY_CALLBACK = "weiboandroidsdk://com.weibo.net.ShareActivity";
  private Button mBackButton;
  private Button mBindButton;
  private EditText mPasswordText;
  private ProgressDialog mProgressDialog;
  private BindWeiboTask mTask;
  protected TaskListener mTaskListener;
  private EditText mUIDText;

  public SinaBindActivity()
  {
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "BindWeiboTask";
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        SinaBindActivity.access$1(this.this$0).dismiss();
        if (paramTaskResult == TaskResult.OK)
        {
          Intent localIntent = new Intent(this.this$0, ShareActivity.class);
          this.this$0.startActivity(localIntent);
          this.this$0.finish();
        }
        while (true)
        {
          return;
          Toast.makeText(this.this$0, SinaBindActivity.access$2(this.this$0).getErrorMessage(), 0).show();
        }
      }

      public void onPreExecute()
      {
        SinaBindActivity.access$0(this.this$0, new ProgressDialog(this.this$0));
        SinaBindActivity.access$1(this.this$0).setMessage("请稍候");
        SinaBindActivity.access$1(this.this$0).show();
      }
    };
  }

  private void bind()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      this.mTask = new BindWeiboTask(this, null);
      this.mTask.setListener(this.mTaskListener);
      TaskParams localTaskParams = new TaskParams();
      localTaskParams.put("user", this.mUIDText.getText().toString());
      localTaskParams.put("password", this.mPasswordText.getText().toString());
      BindWeiboTask localBindWeiboTask = this.mTask;
      TaskParams[] arrayOfTaskParams = new TaskParams[1];
      arrayOfTaskParams[0] = localTaskParams;
      localBindWeiboTask.execute(arrayOfTaskParams);
    }
  }

  private void initView()
  {
    setContentView(2130903044);
    findViewById(2131296344).setVisibility(8);
    this.mBindButton = ((Button)findViewById(2131296278));
    this.mUIDText = ((EditText)findViewById(2131296279));
    this.mPasswordText = ((EditText)findViewById(2131296280));
    this.mBindButton.setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        String str1 = SinaBindActivity.access$3(this.this$0).getText().toString();
        if (!(str1.equalsIgnoreCase("im_qplus")))
          if (StringUtil.isEmpty(str1))
            Toast.makeText(this.this$0, "请先填写邮箱地址", 0).show();
        while (true)
        {
          while (true)
          {
            return;
            String str2 = str1.trim();
            Matcher localMatcher = Regex.EMAIL_ADDRESS_PATTERN.matcher(str2);
            if ((StringUtil.isEmpty(str2)) || (localMatcher.matches()))
              break;
            Toast.makeText(this.this$0, "请先填写邮箱地址", 0).show();
          }
          SinaBindActivity.access$4(this.this$0);
        }
      }
    });
    this.mBackButton = ((Button)findViewById(2131296342));
    this.mBackButton.setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        this.this$0.finish();
      }
    });
  }

  protected void onCreate(Bundle paramBundle)
  {
    requestWindowFeature(1);
    super.onCreate(paramBundle);
    initView();
  }

  protected void onDestroy()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING))
      this.mTask.cancel(true);
    super.onDestroy();
  }

  private class BindWeiboTask extends GenericTask
  {
    private String mErrorMessage;

    protected TaskResult _doInBackground()
    {
      TaskResult localTaskResult;
      TaskParams localTaskParams = paramArrayOfTaskParams[0];
      Weibo localWeibo = Weibo.getInstance();
      localWeibo.setupConsumerConfig("2832649083", "191a6c08a5d3f783a5bd0de5accd180b");
      try
      {
        localTaskParams.getString("user");
        localTaskParams.getString("password");
        RequestToken localRequestToken = localWeibo.getRequestToken(this.this$0, Weibo.APP_KEY, Weibo.APP_SECRET, "weiboandroidsdk://com.weibo.net.ShareActivity");
        AccessToken localAccessToken = localWeibo.generateAccessToken(this.this$0, localRequestToken);
        IKnow.mSystemConfig.setString("com.weibo.android.accesstoken", localAccessToken.getToken());
        IKnow.mSystemConfig.setString("com.weibo.android.token.secret", localAccessToken.getSecret());
        localTaskResult = TaskResult.OK;
        return localTaskResult;
      }
      catch (WeiboException localWeiboException)
      {
        if (localWeiboException.getStatusCode() == 401)
          this.mErrorMessage = "没有进行身份验证";
        do
          while (true)
          {
            while (true)
            {
              localWeiboException.printStackTrace();
              localTaskResult = TaskResult.FAILED;
            }
            if (localWeiboException.getStatusCode() != 40302)
              break;
            this.mErrorMessage = "身份验证失败";
          }
        while (localWeiboException.getStatusCode() != 40309);
        this.mErrorMessage = "密码不正确";
      }
      catch (Exception localException)
      {
        while (true)
          this.mErrorMessage = "网络超时，请稍候再试";
      }
    }

    public String getErrorMessage()
    {
      return this.mErrorMessage;
    }
  }
}